package com.sms.database;

import java.util.ArrayList;
import java.util.List;


import com.sms.model.SMS;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.HashMap;



public class datastore{
	
	private final static String dbname = "SMS";
	private final static String tablename = "history";
	//static final ArrayList<HashMap<String,String>> msg=new ArrayList<HashMap<String,String>>();;
	private final static int version = 2;
	private final static String id = "_id";
	private final static String phonenumber = "phonenumber";
	private final static String message = "message";
	
	private SQLiteDatabase SQLite;
	private Context ctx;
	private DatabaseHelper helper;
	
	private static String createDatabase = "create table history(_id integer primary key Autoincrement, phonenumber text not null,message text not null)";
	
	
	
	public datastore(Context ctx){ this.ctx = ctx; }
	
	public  datastore openDB(){
		helper = new DatabaseHelper(ctx);
		SQLite = helper.getWritableDatabase();
		return this;
	}
	
public void close() 
{ helper.close(); }



public long Insert(SMS s){
		ContentValues values = new ContentValues();
		values.put("phonenumber",s.getPhoneNumber());
		values.put("message", s.getMessage());
		return SQLite.insert(tablename, null, values);
	}

public Cursor fetchAllMessages(int grpId){
	String colNames[]={id,phonenumber,message};
	return SQLite.query(tablename, colNames, id+'='+grpId, null, null, null, null);
}
public boolean removeall()
{
	//SQLite.rawQuery("truncate history;",null);
	return SQLite.delete(tablename, null, null)>0;
	
}
public boolean remove(String row_id)
{
	int rid=Integer.parseInt(row_id);
	return SQLite.delete(tablename, id+"="+rid,null)>0;
	//SQLite.rawQuery("delete * from history where _id= " + rid + ";", null);
}



public ArrayList<SMS> fetchAllMessages()
{
	
	int i=0;
	 System.out.println("aaaaaaaa");
	 Cursor cur=SQLite.query(tablename, new String[] {id, phonenumber, message}, null, null, null, null, null);
	 ArrayList<SMS> msg=new ArrayList<SMS>();
	 if(cur!=null){
		 //HashMap<String,String> temp = new HashMap<String,String>();
		 while(cur.moveToNext())		 {
			 String id1=cur.getString(cur.getColumnIndex(id));
			 String p=cur.getString(cur.getColumnIndex("phonenumber"));
			 String m=cur.getString(cur.getColumnIndex("message"));
			 System.out.println(p+" "+m);
			 
			 SMS sms = new SMS();
			 sms.setId(id1);
			 sms.setMessage(m);
			 sms.setPhoneNumber(p);
			 msg.add(sms);
			 
		 }
	 }
	 
	 
	return msg;
}


public String[] fetchAllMessages1()
{
	
	int i=0;
	 System.out.println("aaaaaaaa");
	 Cursor cur=SQLite.query(tablename, new String[] {id, phonenumber, message}, null, null, null, null, null);
	 String[] msg=new String[cur.getCount()];
	 if(cur!=null){
		 //HashMap<String,String> temp = new HashMap<String,String>();
		 while(cur.moveToNext())		 {
			 
			 String p=cur.getString(cur.getColumnIndex("phonenumber"));
			 String m=cur.getString(cur.getColumnIndex("message"));
			 System.out.println(p+" "+m);
			 
			 msg[i]="To:"+p+"\n"+"Message:\n"+m+"\n";
			 i++;
			 //temp.put("phonenumber", p);
			 
			// temp.put("message",m);
			// msg.add(temp);
		 }
	 }
	 
	 
	return msg;
}

	

}
